// pkgUser/pages/myArticle/myArticle.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    articleList: [],
    queryParms: {
      page: 1,
      limit: 10
    },
    totalPages: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!getApp().token) return wx.redirectTo({
      url: '/pkgUser/pages/login/login',
    })
    this.getArticleList()
  },
  async getArticleList() {
    const { errno, data } = await wx.http.get('/user/myArticle', this.queryParms)
    if (errno !== 0) return wx.toast()
    this.setData({ totalPages: data.totalPages, articleList: data.data })
  },
  async doDel(ev) {
    const id = ev.currentTarget.dataset.id
    const index = ev.currentTarget.dataset.index
    const { errno } = await wx.http.get('/user/delArticle?id=' + id)
    if (errno !== 0) return wx.toast('删除失败！')
    this.data.articleList.splice(index, 1)
    this.setData({ articleList: this.data.articleList })
    console.log(this.data.articleList)
  },
  doEdit(ev) {
    const id = ev.currentTarget.dataset.id
    wx.reLaunch({
      url: '/pages/release/release?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.queryParms.page >= this.data.totalPages) return
    this.data.queryParms.page++
    this.getArticleList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})